DESTDIR =
bindir = /usr/sbin
etcdir = /etc/l2tpns
libdir = /usr/lib/l2tpns
man5dir = /usr/share/man/man5
man8dir = /usr/share/man/man8
statedir = /var/lib/l2tpns

DEFINES =
DEFINES += -DLIBDIR='"$(libdir)"'
DEFINES += -DETCDIR='"$(etcdir)"'

OPTIM =
OPTIM += -g
OPTIM += -O3

CC = gcc
LD = gcc
INCLUDES = -I.
CPPFLAGS = $(INCLUDES) $(DEFINES)
CFLAGS = -Wall -Wformat-security $(OPTIM)
LDFLAGS =
LDLIBS =
INSTALL = install -c -D -o root -g root

l2tpns.LIBS = -lcli -ldl -lm

OBJS = arp.o cli.o cluster.o constants.o control.o icmp.o l2tpns.o \
    ll.o md5.o ppp.o radius.o tbf.o util.o pppoe.o l2tplac.o dhcp6.o ipv6_u.o

PROGRAMS = l2tpns nsctl
PLUGINS = autosnoop.so autothrottle.so garden.so sessionctl.so \
    setrxspeed.so snoopctl.so stripdomain.so throttlectl.so

DEFINES += -DSTATISTICS
DEFINES += -DSTAT_CALLS
DEFINES += -DRINGBUFFER

ifneq (2.4, $(shell uname -r | perl -pe 's/^(\d+\.\d+).*/$$1/'))
 DEFINES += -DHAVE_EPOLL
endif

# DEFINES += -DBGP
# OBJS += bgp.o

all: programs plugins
programs: $(PROGRAMS)
plugins: $(PLUGINS)

clean:
	rm -f *.o test/*.o $(PROGRAMS) $(PLUGINS) Makefile.tmp Makefile.bak

depend:
	(sed -n 'p; /^## Dependencies: (autogenerated) ##/q' Makefile && \
	    gcc -MM $(CPPFLAGS) $(OBJS:.o=.c) && \
	    gcc -MM $(CPPFLAGS) $(PLUGINS:.so=.c) | sed 's/\.o/.so/') >Makefile.tmp
	mv Makefile Makefile.bak
	mv Makefile.tmp Makefile

l2tpns: $(OBJS)
	$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) $($@.LIBS)

nsctl: nsctl.o control.o
	$(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) $($@.LIBS)

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

%.so: %.c
	$(CC) -fPIC -shared $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $<

install: all
	$(INSTALL) -m 0755 l2tpns $(DESTDIR)$(bindir)/l2tpns
	$(INSTALL) -m 0755 nsctl $(DESTDIR)$(bindir)/nsctl

	$(INSTALL) -m 0644 Docs/startup-config.5 $(DESTDIR)$(man5dir)/startup-config.5
	$(INSTALL) -m 0644 Docs/l2tpns.8 $(DESTDIR)$(man8dir)/l2tpns.8
	$(INSTALL) -m 0644 Docs/nsctl.8 $(DESTDIR)$(man8dir)/nsctl.8

	gzip --best --force $(DESTDIR)$(man5dir)/*.5 $(DESTDIR)$(man8dir)/*.8

	@for config in startup-config users ip_pool; \
	do \
	    suffix=; \
	    mode=0600; [ $$config = ip_pool ] && mode=0644; \
	    if [ -f $(DESTDIR)$(etcdir)/$$config ]; \
	    then \
	    	cmp -s etc/$$config.default $(DESTDIR)$(etcdir)/$$config && continue; \
		suffix=.default; \
	    fi; \
	    echo $(INSTALL) -m $$mode etc/$$config.default $(DESTDIR)$(etcdir)/$$config$$suffix; \
	    $(INSTALL) -m $$mode etc/$$config.default $(DESTDIR)$(etcdir)/$$config$$suffix; \
	done

	@for plugin in $(PLUGINS); \
	do \
		echo $(INSTALL) -m 0755 $$plugin $(DESTDIR)$(libdir)/$$plugin; \
		$(INSTALL) -m 0755 $$plugin $(DESTDIR)$(libdir)/$$plugin; \
	done

	@if [ -z $(DESTDIR) ] && [ ! -e /dev/net/tun ]; \
	then \
		mkdir /dev/net; \
		echo mknod /dev/net/tun c 10 200; \
		mknod /dev/net/tun c 10 200; \
	fi

.PHONY: all clean depend install

## Dependencies: (autogenerated) ##
arp.o: arp.c dhcp6.h l2tpns.h
cli.o: cli.c dhcp6.h l2tpns.h constants.h util.h cluster.h tbf.h ll.h \
 bgp.h l2tplac.h
cluster.o: cluster.c dhcp6.h l2tpns.h cluster.h util.h tbf.h pppoe.h \
 bgp.h
constants.o: constants.c constants.h
control.o: control.c dhcp6.h l2tpns.h control.h
icmp.o: icmp.c dhcp6.h l2tpns.h ipv6_u.h
l2tpns.o: l2tpns.c md5.h dhcp6.h l2tpns.h cluster.h plugin.h ll.h \
 constants.h control.h util.h tbf.h bgp.h l2tplac.h pppoe.h
ll.o: ll.c ll.h
md5.o: md5.c md5.h
ppp.o: ppp.c dhcp6.h l2tpns.h constants.h plugin.h util.h tbf.h cluster.h \
 l2tplac.h pppoe.h
radius.o: radius.c md5.h constants.h dhcp6.h l2tpns.h plugin.h util.h \
 cluster.h l2tplac.h pppoe.h
tbf.o: tbf.c dhcp6.h l2tpns.h util.h tbf.h
util.o: util.c dhcp6.h l2tpns.h bgp.h
pppoe.o: pppoe.c dhcp6.h l2tpns.h cluster.h constants.h md5.h util.h
l2tplac.o: l2tplac.c md5.h dhcp6.h l2tpns.h util.h cluster.h l2tplac.h \
 pppoe.h
dhcp6.o: dhcp6.c dhcp6.h l2tpns.h ipv6_u.h
ipv6_u.o: ipv6_u.c ipv6_u.h
bgp.o: bgp.c dhcp6.h l2tpns.h bgp.h util.h
autosnoop.so: autosnoop.c dhcp6.h l2tpns.h plugin.h
autothrottle.so: autothrottle.c dhcp6.h l2tpns.h plugin.h
garden.so: garden.c dhcp6.h l2tpns.h plugin.h control.h
sessionctl.so: sessionctl.c dhcp6.h l2tpns.h plugin.h control.h
setrxspeed.so: setrxspeed.c dhcp6.h l2tpns.h plugin.h
snoopctl.so: snoopctl.c dhcp6.h l2tpns.h plugin.h control.h
stripdomain.so: stripdomain.c dhcp6.h l2tpns.h plugin.h
throttlectl.so: throttlectl.c dhcp6.h l2tpns.h plugin.h control.h
